package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.entity.*;

import com.wanmait.employment.mapper.*;
import com.wanmait.employment.service.CompanyService;
import net.sf.jsqlparser.statement.select.Select;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private CompanyTypeMapper companyTypeMapper;
    @Resource
    private CompanyScaleMapper companyScaleMapper;
    @Resource
    private FinancingStageMapper financingStageMapper;

    @Override
    public PageInfo<Company> findAll(String companyName, Date value1, Date value2, int pageNum, int pageSize) throws ParseException {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();//搜索使用公司昵称 公司账号 公司曾用名
        //                      查找时间
        if(companyName!=null) {
            queryWrapper.and(wrapper -> {
                wrapper.like("company_name", companyName)
                        .or().like("company_account", companyName)
                        .or().like("once_name", companyName);
            });
        }if(value1!=null) {
            queryWrapper.ge("company_register_time", value1)
                    .le("company_register_time", value2);

        }
        List<Company> companyList =companyMapper.selectList(queryWrapper);


        PageInfo<Company> companyPageInfo =new PageInfo<>(companyList);
        companyPageInfo.getList().forEach(company -> {
            List<Address> addressList=addressMapper.selectList(null);
            company.setAddressList(addressList);

            List<CompanyScale>companyScaleList=companyScaleMapper.selectList(null);
            company.setCompanyScaleList(companyScaleList);

            List<CompanyType>companyTypeList=companyTypeMapper.selectList(null);
            company.setCompanyTypeList(companyTypeList);
            //连表查询公司地区
            QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
            addressQueryWrapper.eq("id",company.getAddressId());
            Address address=addressMapper.selectOne(addressQueryWrapper);


            LambdaQueryWrapper<Address> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Address::getId,address.getParentId());
            Address address2=addressMapper.selectOne(queryWrapper1);
            address.setCity(address2);

            LambdaQueryWrapper<Address> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(Address::getId,address2.getParentId());
            Address address3=addressMapper.selectOne(queryWrapper2);
            address.setProvince(address3);
            company.setAddress(address);


            //连表公司类型
            QueryWrapper<CompanyType> companyTypeQueryWrapper = new QueryWrapper<>();
            companyTypeQueryWrapper.eq("id",company.getCompanyTypeId());
            CompanyType companyType=companyTypeMapper.selectOne(companyTypeQueryWrapper);
            company.setCompanyType(companyType);

            //连表查询公司规模
            QueryWrapper<CompanyScale> companyScaleQueryWrapper = new QueryWrapper<>();
            companyScaleQueryWrapper.eq("id", company.getCompanyScaleId());
            CompanyScale scale=companyScaleMapper.selectOne(companyScaleQueryWrapper);
            company.setCompanyScale(scale);

        });

        return companyPageInfo;
    }

    @Override
    public Integer updata(Company company) {
        company.setCompanyScaleId(company.getCompanyScale().getId());
        company.setCompanyTypeId(company.getCompanyType().getId());
        return companyMapper.updateById(company);
    }

    @Override
    public Integer delete(Integer id) {
        return companyMapper.deleteById(id);
    }

    @Override
    public List<Address> getProvince(int parentId) {
        LambdaQueryWrapper<Address> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getParentId,parentId);
        return addressMapper.selectList(queryWrapper);
    }

    @Override
    public Integer resetpasswords(Integer id,String password) {
        return companyMapper.resetpasswords(id,password);
    }

    @Override
    public Integer register(Company company) {
        return companyMapper.insert(company);
    }

    @Override
    public Company login(Company company) {
        if(StringUtils.isBlank(company.getCompanyTel())|| StringUtils.isBlank(company.getCompanyPassword())){
            return null;
        }
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getCompanyTel,company.getCompanyTel());
        queryWrapper.eq(Company::getCompanyPassword,company.getCompanyPassword());
        return companyMapper.selectOne(queryWrapper);
    }



    //    前台公司信息-----宋立新
    @Override
    public Company selectOne(Integer id) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Company company = companyMapper.selectOne(queryWrapper);

//        查询区
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getId,company.getAddressId());
        Address address=addressMapper.selectOne(addressLambdaQueryWrapper);
        company.setAddress(address);
//        查询市
        LambdaQueryWrapper<Address> cityQueryWrapper=new LambdaQueryWrapper<>();
        cityQueryWrapper.eq(Address::getId,address.getParentId());
        Address city=addressMapper.selectOne(cityQueryWrapper);
        company.setCity(city);
//        查询省
        LambdaQueryWrapper<Address> provinceQueryWrapper=new LambdaQueryWrapper<>();
        provinceQueryWrapper.eq(Address::getId,city.getParentId());
        company.setProvince(addressMapper.selectOne(provinceQueryWrapper));

//        连表查询公司规模
            QueryWrapper<CompanyScale> companyScaleQueryWrapper = new QueryWrapper<>();
            companyScaleQueryWrapper.eq("id", company.getCompanyScaleId());
            CompanyScale scale = companyScaleMapper.selectOne(companyScaleQueryWrapper);
            company.setCompanyScale(scale);
//            联表公司类型
            QueryWrapper<CompanyType> companyTypeQueryWrapper = new QueryWrapper<>();
            companyTypeQueryWrapper.eq("id",company.getCompanyTypeId());
            CompanyType companyType =companyTypeMapper.selectOne(companyTypeQueryWrapper);
            company.setCompanyType(companyType);
//            融资阶段
        QueryWrapper<FinancingStage> financingStageQueryWrapper=new QueryWrapper<>();
        financingStageQueryWrapper.eq("id",company.getFinancingStageId());
//        FinancingStage financingStage=financingStageMapper.selectOne(financingStageQueryWrapper);
//        company.setFinancingStage(financingStage);
         company.setFinancingStage(financingStageMapper.selectOne(financingStageQueryWrapper));
        return company;
    }

    @Override
    public Company findByFirmName(String firmName) {
        LambdaQueryWrapper<Company> companyLambdaQueryWrapper=new LambdaQueryWrapper<>();
        companyLambdaQueryWrapper.eq(Company::getFirmName,firmName);
        Company company=companyMapper.selectOne(companyLambdaQueryWrapper);
        return company;
    }

    @Override
    public Integer saveOrUpdate(Company company) {
        if(company.getId()!=null){
            company.setCompanyTypeId(company.getCompanyType().getId());
//            company.setCompanyPic(company.getCompanyPic());
             companyMapper.updateById(company);
        }else{
            company.setCompanyTypeId(company.getCompanyType().getId());
            companyMapper.insert(company);
        }
        return company.getId();
    }


    @Override
    public PageInfo<Company> selectAll(String city,String companyType,String companyScale, String financingStage,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
        if (!city.equals("全国")) {
                    companyQueryWrapper.inSql("address_id", "SELECT id from address where title like '%"+city+"%'"  );
                }
        if (!companyScale.equals("不限")) {
            companyQueryWrapper.inSql("company_scale_id", "SELECT id from company_scale where title like '%"+companyScale+"%'" );
        }
        if (!companyType.equals("不限")) {
            companyQueryWrapper.inSql("company_type_id", "SELECT id from company_type where title like '%"+companyType+"%'");
        }
        if (!financingStage.equals("不限")) {
//            System.out.println(financingStage);
            companyQueryWrapper.inSql("financing_stage_id", "SELECT id from financing_stage where title like '%"+financingStage+"%'");
        }
        List<Company> companyList =companyMapper.selectList(companyQueryWrapper);
        PageInfo<Company> companyPageInfo =new PageInfo<>(companyList);
        companyPageInfo.getList().forEach(company -> {

            //区
            QueryWrapper<Address>  addressQueryWrapper=new QueryWrapper<>();
            addressQueryWrapper.eq("id",company.getAddressId());
            company.setAddress(addressMapper.selectOne(addressQueryWrapper));
            //市
            QueryWrapper<Address>  cityQueryWrapper=new QueryWrapper<>();
            cityQueryWrapper.eq("id",company.getAddress().getParentId());
            company.setCity(addressMapper.selectOne(cityQueryWrapper));

            //省
            QueryWrapper<Address>  provinceQueryWrapper=new QueryWrapper<>();
            provinceQueryWrapper.eq("id",company.getCity().getParentId());
            company.setProvince(addressMapper.selectOne(provinceQueryWrapper));
            //连表查询公司规模
            QueryWrapper<CompanyScale> companyScaleQueryWrapper = new QueryWrapper<>();
            companyScaleQueryWrapper.eq("id", company.getCompanyScaleId());
            CompanyScale scale=companyScaleMapper.selectOne(companyScaleQueryWrapper);
            company.setCompanyScale(scale);

            QueryWrapper<CompanyType> companyTypeQueryWrapper = new QueryWrapper<>();
            companyTypeQueryWrapper.eq("id", company.getCompanyTypeId());
            CompanyType companyType1=companyTypeMapper.selectOne(companyTypeQueryWrapper);
            company.setCompanyType(companyType1);
//          融资阶段
            QueryWrapper<FinancingStage> financingStageQueryWrapper=new QueryWrapper<>();
            financingStageQueryWrapper.eq("id",company.getFinancingStageId());
            company.setFinancingStage(financingStageMapper.selectOne(financingStageQueryWrapper));
        });
        return companyPageInfo;
    }

}
